package com.atguigu.bigdata.redis;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

/**
 * TestRedis 类
 */
@Slf4j
public class TestRedis {

    public static final Integer PORT = 6379;

    // 测试jedis
    static Jedis jedis = new Jedis(Host.LOCALHOST.getHost(), Host.LOCALHOST.getPort());

    public static void main(String[] args) {
        System.out.println(jedis.ping());

//        operate();

        jedis.keys("*").forEach(
                TestRedis::prints
        );
    }

    private static void operate() {
        jedis.flushAll();
        String res1 = jedis.set("k1", "v1");
        String res2 = jedis.setex("k2", 10, "v2");
        log.info("res1: {}", res1);
        log.info("res2: {}", res2);
    }

    private static void prints(String k) {
        System.out.print("key ->" + k);
        System.out.print(", ttl->" + jedis.ttl(k));
        System.out.print(", type->" + jedis.type(k));
        System.out.println(", exists ->" + jedis.exists(k));
        if (jedis.type(k).equals("hash")) {
            System.out.println(jedis.hget(k, "name"));
        } else if (jedis.type(k).equals("zset")) {
            System.out.println(jedis.zrange(k, 0, -1));
        }
    }
}

@AllArgsConstructor
@Getter
enum Host {

    LOCALHOST("127.0.0.1", 6379), RASP_1_29("192.168.1.29", 6379);

    String host;
    int port;
}
